package test;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel.MapMode;

/**
 * 内存映射Memory Map
 */
public class MappedBuffer {
    public static void main(String[] args) throws IOException {
        RandomAccessFile randomAccessFile = new RandomAccessFile("C:\\Users\\Administrator\\Desktop\\test.txt", "rw");
        FileChannel fileChannel = randomAccessFile.getChannel();
        // Memory Map
        MappedByteBuffer mbb = fileChannel.map(MapMode.READ_WRITE, 0, 1024);
        // 97代表字母a
        mbb.put(0, (byte) 97);
        // 122代表字母z
        mbb.put(1023, (byte) 122);
        randomAccessFile.close();
    }
}
